import { useSnippetsStore } from '@renderer/stores'

export function useShortcutKey () {
  const setSearchShortcutError
    = useSnippetsStore(state => state.setSearchShortcutError)
  const setManagerShortcutError
    = useSnippetsStore(state => state.setManagerShortcutError)

  const getSetError = (type: WindowType): (error: string) => void => {
    switch (type) {
      case 'search':
        return setSearchShortcutError
      case 'manager':
        return setManagerShortcutError
      default:
        return () => {}
    }
  }

  const register = async (type: WindowType, shortcutKey: string) => {
    if (!shortcutKey) return

    const res = await window.api.shortcut(type, shortcutKey)
    const setError = getSetError(type)
    if (!res) {
      setError('快捷键注册失败')
    } else {
      setError('')
    }
  }

  return {
    register,
  }
}
